﻿using ExportExcelMethods;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WcfAndMvcSite.WCFServer;

namespace WcfAndMvcSite.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            MyServiceClient client = new MyServiceClient();
            Person person = client.GetSignlePerson();
            return View(person);
        }

        public JsonResult GetAllPerson()
        {
            MyServiceClient client = new MyServiceClient();
            Person[] list = client.GetAllPerson();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public ContentResult GetAgeByName()
        {
            MyServiceClient client = new MyServiceClient();
            ContentResult result = new ContentResult();
            string name = Request.QueryString["name"] ?? string.Empty;
            result.Content = client.GetPersonAgeByName(name);
            return result;
        }

        [HttpPost]
        public ActionResult Index(Person person)
        {
            MyServiceClient client = new MyServiceClient();
            bool bl = client.InsertPerson(person);
            if (bl)
            {
                return View(person);
            }
            return View();
        }

        public ContentResult AddPerson()
        {
            MyServiceClient client = new MyServiceClient();
            ContentResult result = new ContentResult();
            Person person = new Person();
            person.name = Request.Form["name"] ?? string.Empty;
            person.age = Request.Form["age"] ?? string.Empty;
            person.sex = Request.Form["sex"] ?? string.Empty;
            bool bl = client.InsertPerson(person);
            if (bl)
            {
                result.Content = "添加成功";
            }
            else
            {
                result.Content = "添加失败";
            }
            return result;
        }

        public void ExportExcel()
        {
            DataTable dt = new DataTable();

            DataColumn c1 = new DataColumn("aa");
            DataColumn c2 = new DataColumn("bb");
            DataColumn c3 = new DataColumn("cc");

            dt.Columns.Add(c1);
            dt.Columns.Add(c2);
            dt.Columns.Add(c3);
            for (int i = 0; i < 5; i++)
            {
                DataRow r = dt.NewRow(); 
                r["aa"] = "xxxxx---" + i;
                r["bb"] = "wwwww---" + i;
                r["cc"] = "qqqqq---" + i;

                dt.Rows.Add(r);
            }

            ExportExcelMethods.ExportExcelMethods.ExportToExcel(dt, null, "testfile");
        }

        public ActionResult ExportExcelTest()
        {
            return View();
        }
    }
}
